﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHook
{
	public class Breakpoint
	{
		private ProcessDebugger _Debugger;
		private VA _Address;

		public VA Address
		{
			get
			{
				return _Address;
			}
		}

		public Breakpoint(ProcessDebugger debugger, VA address)
		{
			this._Debugger = debugger;
			this._Address = address;
		}

		bool _IsSet = false;
		byte _OldInstruction;
		public void Toggle()
		{
			if(_IsSet)
			{
				_Debugger.WriteMemory(_Address, _OldInstruction);
			}
			else
			{
				_OldInstruction = _Debugger.ReadMemory(_Address);
				_Debugger.WriteMemory(_Address, 0xCC);
			}
			Patch.FlushInstructionCache(_Debugger.Debuggee.Information.ProcessHandle.DangerousGetHandle(), new IntPtr(_Address.Address), (UIntPtr)1);
			_IsSet = !_IsSet;
		}

		public void Enable()
		{
			if(!_IsSet)
				Toggle();
		}
		public void Disable()
		{
			if(_IsSet)
				Toggle();
		}
	}
}
